package com.app.bimo.commonbusiness.view;

import android.os.CountDownTimer;

public class VerificationCountDownTimer extends CountDownTimer {


    public static long curMillis = 0;
    public static boolean FLAG_FIRST_IN = true;

    private Listener listener;

    /**
     * 类中的构造函数
     *
     * @param millisInFuture
     * @param countDownInterval new VerificationCountDownTimer(60 * 1000, 1000);
     */
    public VerificationCountDownTimer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }


    /**
     * 当前任务每完成一次倒计时间隔时间时回调
     *
     * @param millisUntilFinished
     */
    @Override
    public void onTick(long millisUntilFinished) {
        if (listener != null) {
            listener.listener(((int) (millisUntilFinished / 1000) + 1));
        }
    }

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    /**
     * 取消计时
     */
    public void mcancle() {
        cancel();
        VerificationCountDownTimer.FLAG_FIRST_IN = true;
        if (listener != null) {
            listener.finish(true);
        }

    }


    /**
     * 当前任务完成的时候回调
     */
    @Override
    public void onFinish() {
        if (listener != null) {
            listener.finish(false);
        }
    }


    public void timerStart(boolean onClick) {
        if (onClick) {
            VerificationCountDownTimer.curMillis = System.currentTimeMillis();
        }
        VerificationCountDownTimer.FLAG_FIRST_IN = false;
        this.start();
    }

    public interface Listener {
        void listener(int s);

        void finish(boolean isCancel);
    }
}
